library(tidyverse)
library(psych)
library(broom)
library(gridExtra)
library(flextable)
library(stargazer)
library(xtable)

#SETUP ###########################################################################################################################
df1 <- read.csv("data\\Prolific\\survey\\survey.csv") %>%
  filter(consent == 1) %>%
  filter(Finished == 1) %>%
  filter(DistributionChannel == "anonymous") %>%
  filter(party_pre < 9) %>%
  select(-c(StartDate, EndDate, Status, Progress, Duration..in.seconds.,
            Finished, RecordedDate, DistributionChannel, UserLanguage,
            Q_RecaptchaScore, consent, voted_2022)) %>%
  rename(Participant.id = Prolific_ID)

df.demo <-read.csv("data\\Prolific\\demographics.csv") %>%
  select(-c(Submission.id, Status, Custom.study.tncs.accepted.at, 
           Started.at, Completed.at, Reviewed.at, Archived.at, Time.taken,
           Completion.code, Total.approvals))

df1 <- left_join(df1, df.demo,
                 by = "Participant.id") 
df1 <- df1[!is.na(df1$Employment.status), ]
df1 <- df1 %>%
  filter(Age != "CONSENT_REVOKED")

df1$rile_party_pre_1 <- as.numeric(df1$rile_party_pre_1)
df1$rile_party_post_1 <- as.numeric(df1$rile_party_post_1)
df1$rile_respondent_pre_1 <- as.numeric(df1$rile_respondent_pre_1)
df1$rile_respondent_post_1 <- as.numeric(df1$rile_respondent_post_1)
df1$coalition_support_pr <- as.numeric(df1$coalition_support_pr)
df1$support_post <- as.numeric(df1$support_post)
df1$closeness_pre <- as.numeric(df1$closeness_pre)
df1$closeness_post <- as.numeric(df1$closeness_post)
df1$represent_pre <- as.numeric(df1$represent_pre)
df1$represent_post <- as.numeric(df1$represent_post)
df1$pol_interest <- as.numeric(df1$pol_interest)


df1$change_party <- abs(df1$rile_party_pre_1 - df1$rile_party_post_1)
df1$change_self <- abs(df1$rile_respondent_pre_1 - df1$rile_respondent_post_1)
sd(df1$change_party)
mean(df1$change_party)

l <- c("1" = "Fratelli d'Italia",
       "2" = "Lega",
       "3" = "Forza Italia",
       "4" = "IDP",
       "5" = "5-Star Movement",
       "6" = "Azione",
       "7" = "Alleanza Verdi e Sinistra",
       "8" = "+Europa")

ggplot(df1, aes(x = rile_party_pre_1)) +
  geom_density(alpha = 0.6, fill = "skyblue") +
    facet_wrap(~party_pre,  labeller = as_labeller(l)) +
  scale_x_continuous(breaks = seq(0, 10, by = 2)) +
  labs(title = "",
       x = "RILE",
       y = "Density") +
  theme_classic() 


describe(df1)

df1$Condition.num <- df1$Condition
df1$Condition.num <- ifelse(df1$Condition.num == 4, 0,
                        df1$Condition.num)
df1$Condition <- ifelse(df1$Condition == 4, 0,
                        df1$Condition)
df1$Condition <- ifelse(df1$Condition == 1, "Defection",
                        ifelse(df1$Condition == 2, "Compromise",
                        ifelse(df1$Condition == 3, "Defection + Compromise",
                        ifelse(df1$Condition == 0, "Control",
                        df1$Condition))))
df1$Condition <- as.factor(df1$Condition)

df1$Condition <- relevel(df1$Condition, ref = "Control")


###############################################################################################################################
#balance statistics
tab1 <- matrix(NA,5,11)
rownames(tab1) <- c("Age", "Sex", "Ethnicity", "Student Status", "Employment Status")
colnames(tab1) <- c("Mean","SD","Mean","SD","Mean","SD","Mean","SD", "p-Value","p-Value","p-Value")


df1$Age <- as.numeric(as.character(df1$Age))
df1$Sex.num <- as.numeric(factor(df1$Sex))
df1$Ethnicity.simplified.num <- as.numeric(factor(df1$Ethnicity.simplified))
df1$Student.status.num <- as.numeric(factor(df1$Student.status))
df1$Employment.status.num <- as.numeric(factor(df1$Employment.status))

tab1[1,1] <- mean(df1$Age[df1$Condition.num == 0])
tab1[1,3] <- mean(df1$Age[df1$Condition.num == 1])
tab1[1,5] <- mean(df1$Age[df1$Condition.num == 2])
tab1[1,7] <- mean(df1$Age[df1$Condition.num == 3])


tab1[2,1] <- mean(df1$Sex.num[df1$Condition.num == 0])
tab1[2,3] <- mean(df1$Sex.num[df1$Condition.num == 1])
tab1[2,5] <- mean(df1$Sex.num[df1$Condition.num == 2])
tab1[2,7] <- mean(df1$Sex.num[df1$Condition.num == 3])

tab1[3,1] <- mean(df1$Ethnicity.simplified.num[df1$Condition.num == 0])
tab1[3,3] <- mean(df1$Ethnicity.simplified.num[df1$Condition.num == 1])
tab1[3,5] <- mean(df1$Ethnicity.simplified.num[df1$Condition.num == 2])
tab1[3,7] <- mean(df1$Ethnicity.simplified.num[df1$Condition.num == 3])

tab1[4,1] <- mean(df1$Student.status.num[df1$Condition.num == 0])
tab1[4,3] <- mean(df1$Student.status.num[df1$Condition.num == 1])
tab1[4,5] <- mean(df1$Student.status.num[df1$Condition.num == 2])
tab1[4,7] <- mean(df1$Student.status.num[df1$Condition.num == 3])

tab1[5,1] <- mean(df1$Employment.status.num[df1$Condition.num == 0])
tab1[5,3] <- mean(df1$Employment.status.num[df1$Condition.num == 1])
tab1[5,5] <- mean(df1$Employment.status.num[df1$Condition.num == 2])
tab1[5,7] <- mean(df1$Employment.status.num[df1$Condition.num == 3])

tab1[1,2] <- sd(df1$Age[df1$Condition.num == 0])
tab1[1,4] <- sd(df1$Age[df1$Condition.num == 1])
tab1[1,6] <- sd(df1$Age[df1$Condition.num == 2])
tab1[1,8] <- sd(df1$Age[df1$Condition.num == 3])

tab1[2,2] <- sd(df1$Sex.num[df1$Condition.num == 0])
tab1[2,4] <- sd(df1$Sex.num[df1$Condition.num == 1])
tab1[2,6] <- sd(df1$Sex.num[df1$Condition.num == 2])
tab1[2,8] <- sd(df1$Sex.num[df1$Condition.num == 3])

tab1[3,2] <- sd(df1$Ethnicity.simplified.num[df1$Condition.num == 0])
tab1[3,4] <- sd(df1$Ethnicity.simplified.num[df1$Condition.num == 1])
tab1[3,6] <- sd(df1$Ethnicity.simplified.num[df1$Condition.num == 2])
tab1[3,8] <- sd(df1$Ethnicity.simplified.num[df1$Condition.num == 3])

tab1[4,2] <- sd(df1$Student.status.num[df1$Condition.num == 0])
tab1[4,4] <- sd(df1$Student.status.num[df1$Condition.num == 1])
tab1[4,6] <- sd(df1$Student.status.num[df1$Condition.num == 2])
tab1[4,8] <- sd(df1$Student.status.num[df1$Condition.num == 3])

tab1[5,2] <- sd(df1$Employment.status.num[df1$Condition.num == 0])
tab1[5,4] <- sd(df1$Employment.status.num[df1$Condition.num == 1])
tab1[5,6] <- sd(df1$Employment.status.num[df1$Condition.num == 2])
tab1[5,8] <- sd(df1$Employment.status.num[df1$Condition.num == 3])

mean(df1$pol_interest[df1$Condition.num == 0])
mean(df1$pol_interest[df1$Condition.num == 1])
mean(df1$pol_interest[df1$Condition.num == 2])
mean(df1$pol_interest[df1$Condition.num == 3])
sd(df1$pol_interest[df1$Condition.num == 0])
sd(df1$pol_interest[df1$Condition.num == 1])
sd(df1$pol_interest[df1$Condition.num == 2])
sd(df1$pol_interest[df1$Condition.num == 3])


control_group_age <- df1[df1$Condition == "Control", "Age"]
comp_group_age <-  df1[df1$Condition == "Compromise", "Age"]
p.age <- t.test(control_group_age, comp_group_age)
tab1[1,9] <- p.age$p.value

fail_group_age <-  df1[df1$Condition == "Defection", "Age"]
p.age <- t.test(control_group_age, fail_group_age)
tab1[1,10] <- p.age$p.value

failcomp_group_age <-  df1[df1$Condition == "Defection + Compromise", "Age"]
p.age <- t.test(control_group_age, failcomp_group_age)
tab1[1,11] <- p.age$p.value



control_group_sex <- df1[df1$Condition == "Control", "Sex.num"]
comp_group_sex <-  df1[df1$Condition == "Compromise", "Sex.num"]
p.sex <- t.test(control_group_sex, comp_group_sex)
tab1[2,9] <- p.sex$p.value

fail_group_sex <-  df1[df1$Condition == "Defection", "Sex.num"]
p.sex <- t.test(control_group_sex, fail_group_sex)
tab1[2,10] <- p.sex$p.value

failcomp_group_sex <-  df1[df1$Condition == "Defection + Compromise", "Sex.num"]
p.sex <- t.test(control_group_sex, failcomp_group_sex)
tab1[2,11] <- p.sex$p.value




control_group_eth <- df1[df1$Condition == "Control", "Ethnicity.simplified.num"]
comp_group_eth <-  df1[df1$Condition == "Compromise", "Ethnicity.simplified.num"]
p.eth <- t.test(control_group_eth, comp_group_eth)
tab1[3,9] <- p.eth$p.value

fail_group_eth <-  df1[df1$Condition == "Defection", "Ethnicity.simplified.num"]
p.sex <- t.test(control_group_eth, fail_group_eth)
tab1[3,10] <- p.eth$p.value

failcomp_group_eth <-  df1[df1$Condition == "Defection + Compromise", "Ethnicity.simplified.num"]
p.sex <- t.test(control_group_eth, failcomp_group_eth)
tab1[3,11] <- p.eth$p.value



control_group_stu <- df1[df1$Condition == "Control", "Student.status.num"]
comp_group_stu <-  df1[df1$Condition == "Compromise", "Student.status.num"]
p.stu <- t.test(control_group_stu, comp_group_stu)
tab1[4,9] <- p.stu$p.value

fail_group_stu <-  df1[df1$Condition == "Defection", "Student.status.num"]
p.stu <- t.test(control_group_stu, fail_group_stu)
tab1[4,10] <- p.stu$p.value

failcomp_group_stu <-  df1[df1$Condition == "Defection + Compromise", "Student.status.num"]
p.stu <- t.test(control_group_stu, failcomp_group_stu)
tab1[4,11] <- p.stu$p.value



control_group_emp <- df1[df1$Condition == "Control", "Employment.status.num"]
comp_group_emp <-  df1[df1$Condition == "Compromise", "Employment.status.num"]
p.emp <- t.test(control_group_emp, comp_group_emp)
tab1[5,9] <- p.emp$p.value

fail_group_emp <-  df1[df1$Condition == "Defection", "Employment.status.num"]
p.emp <- t.test(control_group_emp, fail_group_emp)
tab1[5,10] <- p.emp$p.value

failcomp_group_emp <-  df1[df1$Condition == "Defection + Compromise", "Employment.status.num"]
p.emp <- t.test(control_group_emp, failcomp_group_emp)
tab1[5,11] <- p.emp$p.value


control_group_intr <-  df1[df1$Condition == "Control", "pol_interest"]
comp_group_intr <-  df1[df1$Condition == "Compromise", "pol_interest"]
p.emp <- t.test(control_group_intr, comp_group_intr)
p.emp$p.value

fail_group_intr <-  df1[df1$Condition == "Defection", "pol_interest"]
p.emp <- t.test(control_group_intr, fail_group_intr)
p.emp$p.value

failcomp_group_intr <-  df1[df1$Condition == "Defection + Compromise", "pol_interest"]
p.emp <- t.test(control_group_intr, failcomp_group_intr)
p.emp$p.value

latex_table <- xtable(tab1, caption = "Balance Statistics for Control and Treatment Groups", label = "tab:balance")
print(latex_table, type = "latex", file = "balance_statistics_table.tex", include.rownames = FALSE)
latex_table

tab1 <- as.data.frame(tab1)

#######################################################################################################################
#pairwise t tests with bonferroni correction for multiple comparisons
df2 <- df1 %>%
  na.omit() %>%
  select(c(rile_party_pre_1, Condition))
df3 <- df1 %>%
  na.omit() %>%
  select(c(rile_party_post_1, Condition))

df2_wide <- df2 %>%
  mutate(row = row_number()) %>%
  tidyr::pivot_wider(names_from = Condition, values_from = rile_party_pre_1) 
df2_wide$row <- as.numeric(df2_wide$row)

df3_wide <- df3 %>%
  mutate(row = row_number()) %>%
  tidyr::pivot_wider(names_from = Condition, values_from = rile_party_post_1) 
df3_wide$row <- as.numeric(df3_wide$row)


df4 <- left_join(df2_wide, df3_wide,
                 by = "row")

paired_tests <- list(
  "Control" = t.test(df4$Control.x, df4$Control.y, paired = TRUE),
  "Compromise" = t.test(df4$Compromise.x, df4$Compromise.y, paired = TRUE),
  "Defection" = t.test(df4$Defection.x, df4$Defection.y, paired = TRUE),
  "Defection + Compromise" = t.test(df4$`Defection + Compromise.x`, df4$`Defection + Compromise.y`, paired = TRUE)
)

# Extract p-values from the paired t-tests
p_values <- sapply(paired_tests, function(x) x$p.value)

# Apply Bonferroni correction
p_values_bonferroni <- p.adjust(p_values, method = "bonferroni")

# Combine results into a data frame
results <- data.frame(
  Condition = names(paired_tests),
  Estimate = sapply(paired_tests, function(x) x$estimate),
  P_Value = p_values,
  P_Value_Bonferroni = p_values_bonferroni
)

# Print the results
print(results)

results_formatted <- results %>%
  mutate(
    Estimate = round(Estimate, 3),
    P_Value = round(P_Value, 4),
    p_value_adj = round(P_Value_Bonferroni, 4)
  )
print(results_formatted)

##############
#proportion of partisans in sample
parties <- df1 %>%
  group_by(party_pre) %>%
  summarise(count = n())


################################################################################################################
#linear model
model1 <- lm(change_party ~ Condition,
             data = df1)
summary(model1)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

tidy_reg_conf_int <- df1 %>% 
  lm(change_party ~ Condition,
               data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int


plot1 <- tidy_reg_conf_int %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1

#plot with 90% and 95% CI's

df95 <- tidy(model1, conf.int = T,
             conf.level = 0.95)
df90 <- tidy(model1, conf.int = T,
             conf.level = 0.90)
coef_df <- merge(df95, df90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
plot1<- coef_df %>%
  filter(term != "(Intercept)") %>%
ggplot(aes(x = estimate, y = term)) +
  geom_point(size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +# 90% CI  # Flip for horizontal plot
  labs(title = "",
       x = "",
       y = "") +
  scale_y_discrete(labels = l) +
theme_classic() 
plot1



#linear model with controls
model2 <- lm(change_party ~ Condition +
               rile_respondent_pre_1 +
               rile_party_pre_1 +
               Age +
               Sex.num +
               pol_interest +
               Ethnicity.simplified.num +
               Student.status.num +
               Employment.status.num +
               coalition_support_pr +
               closeness_pre +
               represent_pre,
             data = df1)
summary(model2)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  lm(change_party ~ Condition +
       rile_respondent_pre_1 +
       rile_party_pre_1 +
       Age +
       Sex.num +
       pol_interest +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num +
       coalition_support_pr +
       closeness_pre +
       represent_pre,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
        "Sex.num" = "Sex",
         "Ethnicity.simplified.num" = "Ethnicity",
         "Student.status.num" = "Student",
         "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_2 <- tidy(model2, conf.int = T,
               conf.level = 0.95)
df90_2 <- tidy(model2, conf.int = T,
               conf.level = 0.90)
coef_df_2 <- merge(df95_2, df90_2[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot2 <- coef_df_2 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2

grid.arrange(plot1, plot2, ncol=2,
             top = "",
             bottom = "Treatment Effect") 




#################
#multiple comparissons 
summary_model <- summary(model2)
p_values <- summary_model$coefficients[, "Pr(>|t|)"]

# Apply Bonferroni correction
p_values_bonferroni <- p.adjust(p_values, method = "bonferroni")

# Apply Holm's correction
p_values_holm <- p.adjust(p_values, method = "holm")

# Apply FDR correction
p_values_fdr <- p.adjust(p_values, method = "fdr")

# Combine the results into a data frame for comparison
results <- data.frame(
  Term = rownames(summary_model$coefficients),
  Original_P_Values = p_values,
  Bonferroni = p_values_bonferroni,
  Holm = p_values_holm,
  FDR = p_values_fdr
)

# View the results
print(results)



#are coefficients statistically different?
library(car)
linearHypothesis(model2, c("ConditionDefection - ConditionDefection + Compromise = 0"))
linearHypothesis(model2, c("ConditionCompromise - ConditionDefection + Compromise = 0"))
linearHypothesis(model2, c("ConditionDefection - ConditionCompromise = 0"))

library(marginaleffects)
#hypos 1 and 2
c <- comparisons(model2, 
                 variables = list(Condition = "reference"), #what to compare to each other, creates contrasts across values
                 newdata = "mean",
                 hypothesis = "b1 = b2",
                # hypothesis = "b2 = b3",
                # hypothesis = "b1 = b3",
                 p_adjust = "bonferroni") %>% 
  glimpse()




#############################################################################################################3


#other outcomes, paired t-tests
#create samples of each condition 
df.control.support.pre <- df1 %>%
  filter(df1$Condition == "Control") 
df.control.support.post <- df1 %>%
  filter(df1$Condition == "Control")

df.comp.support.pre <- df1 %>%
  filter(df1$Condition == "Compromise") 
df.comp.support.post <- df1 %>%
  filter(df1$Condition == "Compromise")

df.fail.support.pre <- df1 %>%
  filter(df1$Condition == "Defection") 
df.fail.support.post <- df1 %>%
  filter(df1$Condition == "Defection")

df.failcomp.support.pre <- df1 %>%
  filter(df1$Condition == "Defection + Compromise") 
df.failcomp.support.post <- df1 %>%
  filter(df1$Condition == "Defection + Compromise")

#support paired t-test
control1 <- t.test(df.control.support.pre$coalition_support_pr, df.control.support.post$support_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
control1

comp1 <- t.test(df.comp.support.pre$coalition_support_pr, df.comp.support.post$support_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
comp1

fail1 <- t.test(df.fail.support.pre$coalition_support_pr, df.fail.support.post$support_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
fail1

failcomp1 <- t.test(df.failcomp.support.pre$coalition_support_pr, df.failcomp.support.post$support_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
failcomp1


#closeness paired t-test
df.control.close.pre <- df1 %>%
  filter(df1$Condition == "Control") 
df.control.close.post <- df1 %>%
  filter(df1$Condition == "Control")

df.comp.close.pre <- df1 %>%
  filter(df1$Condition == "Compromise") 
df.comp.close.post <- df1 %>%
  filter(df1$Condition == "Compromise")

df.fail.close.pre <- df1 %>%
  filter(df1$Condition == "Defection") 
df.fail.close.post <- df1 %>%
  filter(df1$Condition == "Defection")

df.failcomp.close.pre <- df1 %>%
  filter(df1$Condition == "Defection + Compromise") 
df.failcomp.close.post <- df1 %>%
  filter(df1$Condition == "Defection + Compromise")

control2 <- t.test(df.control.close.pre$closeness_pre, df.control.close.post$closeness_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
control2

comp2 <- t.test(df.comp.close.pre$closeness_pre, df.comp.close.post$closeness_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
comp2

fail2 <- t.test(df.fail.close.pre$closeness_pre, df.fail.close.post$closeness_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
fail2

failcomp2 <- t.test(df.failcomp.close.pre$closeness_pre, df.failcomp.close.post$closeness_post,
       paired = T,
       alternative = "two.sided",
       p.adjust.method = "bonferroni")
failcomp2






#representativeness paired t-test
df.control.rep.pre <- df1 %>%
  filter(df1$Condition == "Control") 
df.control.rep.post <- df1 %>%
  filter(df1$Condition == "Control")

df.comp.rep.pre <- df1 %>%
  filter(df1$Condition == "Compromise") 
df.comp.rep.post <- df1 %>%
  filter(df1$Condition == "Compromise")

df.fail.rep.pre <- df1 %>%
  filter(df1$Condition == "Defection") 
df.fail.rep.post <- df1 %>%
  filter(df1$Condition == "Defection")

df.failcomp.rep.pre <- df1 %>%
  filter(df1$Condition == "Defection + Compromise") 
df.failcomp.rep.post <- df1 %>%
  filter(df1$Condition == "Defection + Compromise")

control3 <- t.test(df.control.rep.pre$represent_pre, df.control.rep.post$represent_post,
                   paired = T,
                   alternative = "two.sided",
                   p.adjust.method = "bonferroni")
control3

comp3 <- t.test(df.comp.rep.pre$represent_pre, df.comp.rep.post$represent_post,
                paired = T,
                alternative = "two.sided",
                p.adjust.method = "bonferroni")
comp3

fail3 <- t.test(df.fail.rep.pre$represent_pre, df.fail.rep.post$represent_post,
                paired = T,
                alternative = "two.sided",
                p.adjust.method = "bonferroni")
fail3

failcomp3 <- t.test(df.failcomp.rep.pre$represent_pre, df.failcomp.rep.post$represent_post,
                    paired = T,
                    alternative = "two.sided",
                    p.adjust.method = "bonferroni")
failcomp3







tab1 <- map_df(list(control1, fail1, comp1, failcomp1), tidy)
tab1 %>%
  select(-c(conf.high, conf.low, method, alternative, parameter)) %>%
  mutate(condition = c("Control", "Defection", "Compromise", "Defection + Compromise")) %>%
  flextable()%>% 
  add_header_lines(values = "Paired t-Tests for Coalition Support (One-Tail)")

tab2 <- map_df(list(control2, fail2, comp2, failcomp2), tidy)
tab2 %>%
  select(-c(conf.high, conf.low, method, alternative, parameter)) %>%
  mutate(condition = c("Control", "Defection", "Compromise", "Defection + Compromise")) %>%
  flextable() %>% 
  add_header_lines(values = "Paired t-Tests for Party Closeness (One-Tail)")

tab3 <- map_df(list(control3, fail3, comp3, failcomp3), tidy)
tab3 %>%
  select(-c(conf.high, conf.low, method, alternative, parameter)) %>%
  mutate(condition = c("Control", "Defection", "Compromise", "Defection + Compromise")) %>%
  flextable() %>% 
  add_header_lines(values = "Paired t-Tests for Party Representativeness (One-Tail)")


#support effects by party #################################################################################
df.control <- df1 %>%
  filter(df1$Condition == "Control")
df.comp <- df1 %>%
  filter(df1$Condition == "Compromise")
df.fail <- df1 %>%
  filter(df1$Condition == "Defection")
df.failcomp <- df1 %>%
  filter(df1$Condition == "Defection + Compromise")
         
df.control.party1 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "1") 
df.control.party2 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "2") 
df.control.party3 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "3") 
df.control.party4 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "4") 
df.control.party5 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "5") 
df.control.party6 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "6") 
df.control.party7 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "7") 
df.control.party8 <- df1 %>%
  filter(df1$Condition == "Control" & df1$party_pre == "8") 

support_control_party1 <- (t.test(df.control.party1$coalition_support_pr, df.control.party1$support_post, paired = TRUE))
support_control_party1
support_control_party2 <- (t.test(df.control.party2$coalition_support_pr, df.control.party2$support_post, paired = TRUE))
support_control_party2
support_control_party3 <- (t.test(df.control.party3$coalition_support_pr, df.control.party3$support_post, paired = TRUE))
support_control_party3
support_control_party4 <- (t.test(df.control.party4$coalition_support_pr, df.control.party4$support_post, paired = TRUE))
support_control_party4
support_control_party5 <- (t.test(df.control.party5$coalition_support_pr, df.control.party5$support_post, paired = TRUE))
support_control_party5
support_control_party6 <- (t.test(df.control.party6$coalition_support_pr, df.control.party6$support_post, paired = TRUE))
support_control_party6
support_control_party7 <- (t.test(df.control.party7$coalition_support_pr, df.control.party7$support_post, paired = TRUE))
support_control_party7
support_control_party8 <- (t.test(df.control.party8$coalition_support_pr, df.control.party8$support_post, paired = TRUE))
support_control_party8



df.comp.party1 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "1") 
df.comp.party2 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "2") 
df.comp.party3 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "3") 
df.comp.party4 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "4") 
df.comp.party5 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "5") 
df.comp.party6 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "6") 
df.comp.party7 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "7") 
df.comp.party8 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "8") 

support_comp_party1 <- (t.test(df.comp.party1$coalition_support_pr, df.comp.party1$support_post, paired = TRUE))
support_comp_party1
support_comp_party2 <- (t.test(df.comp.party2$coalition_support_pr, df.comp.party2$support_post, paired = TRUE))
support_comp_party2
support_comp_party3 <- (t.test(df.comp.party3$coalition_support_pr, df.comp.party3$support_post, paired = TRUE))
support_comp_party3
support_comp_party4 <- (t.test(df.comp.party4$coalition_support_pr, df.comp.party4$support_post, paired = TRUE))
support_comp_party4
support_comp_party5 <- (t.test(df.comp.party5$coalition_support_pr, df.comp.party5$support_post, paired = TRUE))
support_comp_party5
support_comp_party6 <- (t.test(df.comp.party6$coalition_support_pr, df.comp.party6$support_post, paired = TRUE))
support_comp_party6
support_comp_party7 <- (t.test(df.comp.party7$coalition_support_pr, df.comp.party7$support_post, paired = TRUE))
support_comp_party7
support_comp_party8 <- (t.test(df.comp.party8$coalition_support_pr, df.comp.party8$support_post, paired = TRUE))
support_comp_party8


df.fail.party1 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "1") 
df.fail.party2 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "2") 
df.fail.party3 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "3") 
df.fail.party4 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "4") 
df.fail.party5 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "5") 
df.fail.party6 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "6") 
df.fail.party7 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "7") 
df.fail.party8 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "8") 

support_fail_party1 <- (t.test(df.fail.party1$coalition_support_pr, df.fail.party1$support_post, paired = TRUE))
support_fail_party1
support_fail_party2 <- (t.test(df.fail.party2$coalition_support_pr, df.fail.party2$support_post, paired = TRUE))
support_fail_party2
support_fail_party3 <- (t.test(df.fail.party3$coalition_support_pr, df.fail.party3$support_post, paired = TRUE))
support_fail_party3
support_fail_party4 <- (t.test(df.fail.party4$coalition_support_pr, df.fail.party4$support_post, paired = TRUE))
support_fail_party4
support_fail_party5 <- (t.test(df.fail.party5$coalition_support_pr, df.fail.party5$support_post, paired = TRUE))
support_fail_party5
support_fail_party6 <- (t.test(df.fail.party6$coalition_support_pr, df.fail.party6$support_post, paired = TRUE))
support_fail_party6
support_fail_party7 <- (t.test(df.fail.party7$coalition_support_pr, df.fail.party7$support_post, paired = TRUE))
support_fail_party7
support_fail_party8 <- (t.test(df.fail.party8$coalition_support_pr, df.fail.party8$support_post, paired = TRUE))
support_fail_party8



df.failcomp.party1 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "1") 
df.failcomp.party2 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "2") 
df.failcomp.party3 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "3") 
df.failcomp.party4 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "4") 
df.failcomp.party5 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "5") 
df.failcomp.party6 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "6") 
df.failcomp.party7 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "7") 
df.failcomp.party8 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "8") 

support_failcomp_party1 <- (t.test(df.failcomp.party1$coalition_support_pr, df.failcomp.party1$support_post, paired = TRUE))
support_failcomp_party1
support_failcomp_party2 <- (t.test(df.failcomp.party2$coalition_support_pr, df.failcomp.party2$support_post, paired = TRUE))
support_failcomp_party2
support_failcomp_party3 <- (t.test(df.failcomp.party3$coalition_support_pr, df.failcomp.party3$support_post, paired = TRUE))
support_failcomp_party3
support_failcomp_party4 <- (t.test(df.failcomp.party4$coalition_support_pr, df.failcomp.party4$support_post, paired = TRUE))
support_failcomp_party4
support_failcomp_party5 <- (t.test(df.failcomp.party5$coalition_support_pr, df.failcomp.party5$support_post, paired = TRUE))
support_failcomp_party5
support_failcomp_party6 <- (t.test(df.failcomp.party6$coalition_support_pr, df.failcomp.party6$support_post, paired = TRUE))
support_failcomp_party6
support_failcomp_party7 <- (t.test(df.failcomp.party7$coalition_support_pr, df.failcomp.party7$support_post, paired = TRUE))
support_failcomp_party7
support_failcomp_party8 <- (t.test(df.failcomp.party8$coalition_support_pr, df.failcomp.party8$support_post, paired = TRUE))
support_failcomp_party8

support_control_combined_results <- df.control %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(coalition_support_pr, support_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(support_control_combined_results)

support_comp_combined_results <- df.comp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(coalition_support_pr, support_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(support_comp_combined_results)

support_fail_combined_results <- df.fail %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(coalition_support_pr, support_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(support_fail_combined_results)

support_failcomp_combined_results <- df.failcomp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(coalition_support_pr, support_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(support_failcomp_combined_results)

#closeness effects by party #################################################################################
close_control_party1 <- (t.test(df.control.party1$closeness_pre, df.control.party1$closeness_post, paired = TRUE))
close_control_party1
close_control_party2 <- (t.test(df.control.party2$closeness_pre, df.control.party2$closeness_post, paired = TRUE))
close_control_party2
close_control_party3 <- (t.test(df.control.party3$closeness_pre, df.control.party3$closeness_post, paired = TRUE))
close_control_party3
close_control_party4 <- (t.test(df.control.party4$closeness_pre, df.control.party4$closeness_post, paired = TRUE))
close_control_party4
close_control_party5 <- (t.test(df.control.party5$closeness_pre, df.control.party5$closeness_post, paired = TRUE))
close_control_party5
close_control_party6 <- (t.test(df.control.party6$closeness_pre, df.control.party6$closeness_post, paired = TRUE))
close_control_party6
close_control_party7 <- (t.test(df.control.party7$closeness_pre, df.control.party7$closeness_post, paired = TRUE))
close_control_party7
close_control_party8 <- (t.test(df.control.party8$closeness_pre, df.control.party8$closeness_post, paired = TRUE))
close_control_party8



df.comp.party1 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "1") 
df.comp.party2 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "2") 
df.comp.party3 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "3") 
df.comp.party4 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "4") 
df.comp.party5 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "5") 
df.comp.party6 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "6") 
df.comp.party7 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "7") 
df.comp.party8 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "8") 

close_comp_party1 <- (t.test(df.comp.party1$closeness_pre, df.comp.party1$closeness_post, paired = TRUE))
close_comp_party1
close_comp_party2 <- (t.test(df.comp.party2$closeness_pre, df.comp.party2$closeness_post, paired = TRUE))
close_comp_party2
close_comp_party3 <- (t.test(df.comp.party3$closeness_pre, df.comp.party3$closeness_post, paired = TRUE))
close_comp_party3
close_comp_party4 <- (t.test(df.comp.party4$closeness_pre, df.comp.party4$closeness_post, paired = TRUE))
close_comp_party4
close_comp_party5 <- (t.test(df.comp.party5$closeness_pre, df.comp.party5$closeness_post, paired = TRUE))
close_comp_party5
close_comp_party6 <- (t.test(df.comp.party6$closeness_pre, df.comp.party6$closeness_post, paired = TRUE))
close_comp_party6
close_comp_party7 <- (t.test(df.comp.party7$closeness_pre, df.comp.party7$closeness_post, paired = TRUE))
close_comp_party7
close_comp_party8 <- (t.test(df.comp.party8$closeness_pre, df.comp.party8$closeness_post, paired = TRUE))
close_comp_party8


df.fail.party1 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "1") 
df.fail.party2 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "2") 
df.fail.party3 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "3") 
df.fail.party4 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "4") 
df.fail.party5 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "5") 
df.fail.party6 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "6") 
df.fail.party7 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "7") 
df.fail.party8 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "8") 

close_fail_party1 <- (t.test(df.fail.party1$closeness_pre, df.fail.party1$closeness_post, paired = TRUE))
close_fail_party1
close_fail_party2 <- (t.test(df.fail.party2$closeness_pre, df.fail.party2$closeness_post, paired = TRUE))
close_fail_party2
close_fail_party3 <- (t.test(df.fail.party3$closeness_pre, df.fail.party3$closeness_post, paired = TRUE))
close_fail_party3
close_fail_party4 <- (t.test(df.fail.party4$closeness_pre, df.fail.party4$closeness_post, paired = TRUE))
close_fail_party4
close_fail_party5 <- (t.test(df.fail.party5$closeness_pre, df.fail.party5$closeness_post, paired = TRUE))
close_fail_party5
close_fail_party6 <- (t.test(df.fail.party6$closeness_pre, df.fail.party6$closeness_post, paired = TRUE))
close_fail_party6
close_fail_party7 <- (t.test(df.fail.party7$closeness_pre, df.fail.party7$closeness_post, paired = TRUE))
close_fail_party7
close_fail_party8 <- (t.test(df.fail.party8$closeness_pre, df.fail.party8$closeness_post, paired = TRUE))
close_fail_party8



df.failcomp.party1 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "1") 
df.failcomp.party2 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "2") 
df.failcomp.party3 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "3") 
df.failcomp.party4 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "4") 
df.failcomp.party5 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "5") 
df.failcomp.party6 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "6") 
df.failcomp.party7 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "7") 
df.failcomp.party8 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "8") 

close_failcomp_party1 <- (t.test(df.failcomp.party1$closeness_pre, df.failcomp.party1$closeness_post, paired = TRUE))
close_failcomp_party1
close_failcomp_party2 <- (t.test(df.failcomp.party2$closeness_pre, df.failcomp.party2$closeness_post, paired = TRUE))
close_failcomp_party2
close_failcomp_party3 <- (t.test(df.failcomp.party3$closeness_pre, df.failcomp.party3$closeness_post, paired = TRUE))
close_failcomp_party3
close_failcomp_party4 <- (t.test(df.failcomp.party4$closeness_pre, df.failcomp.party4$closeness_post, paired = TRUE))
close_failcomp_party4
close_failcomp_party5 <- (t.test(df.failcomp.party5$closeness_pre, df.failcomp.party5$closeness_post, paired = TRUE))
close_failcomp_party5
close_failcomp_party6 <- (t.test(df.failcomp.party6$closeness_pre, df.failcomp.party6$closeness_post, paired = TRUE))
close_failcomp_party6
close_failcomp_party7 <- (t.test(df.failcomp.party7$closeness_pre, df.failcomp.party7$closeness_post, paired = TRUE))
close_failcomp_party7
close_failcomp_party8 <- (t.test(df.failcomp.party8$closeness_pre, df.failcomp.party8$closeness_post, paired = TRUE))
close_failcomp_party8

close_control_combined_results <- df.control %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(closeness_pre, closeness_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(close_control_combined_results)

close_comp_combined_results <- df.comp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(closeness_pre, closeness_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(close_comp_combined_results)

close_fail_combined_results <- df.fail %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(closeness_pre, closeness_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(close_fail_combined_results)

close_failcomp_combined_results <- df.failcomp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(closeness_pre, closeness_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(close_failcomp_combined_results)


#representation effects by party #################################################################################
rep_control_party1 <- (t.test(df.control.party1$represent_pre, df.control.party1$represent_post, paired = TRUE))
rep_control_party1
rep_control_party2 <- (t.test(df.control.party2$represent_pre, df.control.party2$represent_post, paired = TRUE))
rep_control_party2
rep_control_party3 <- (t.test(df.control.party3$represent_pre, df.control.party3$represent_post, paired = TRUE))
rep_control_party3
rep_control_party4 <- (t.test(df.control.party4$represent_pre, df.control.party4$represent_post, paired = TRUE))
rep_control_party4
rep_control_party5 <- (t.test(df.control.party5$represent_pre, df.control.party5$represent_post, paired = TRUE))
rep_control_party5
rep_control_party6 <- (t.test(df.control.party6$represent_pre, df.control.party6$represent_post, paired = TRUE))
rep_control_party6
rep_control_party7 <- (t.test(df.control.party7$represent_pre, df.control.party7$represent_post, paired = TRUE))
rep_control_party7
rep_control_party8 <- (t.test(df.control.party8$represent_pre, df.control.party8$represent_post, paired = TRUE))
rep_control_party8



df.comp.party1 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "1") 
df.comp.party2 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "2") 
df.comp.party3 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "3") 
df.comp.party4 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "4") 
df.comp.party5 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "5") 
df.comp.party6 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "6") 
df.comp.party7 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "7") 
df.comp.party8 <- df1 %>%
  filter(df1$Condition == "Compromise" & df1$party_pre == "8") 

rep_comp_party1 <- (t.test(df.comp.party1$represent_pre, df.comp.party1$represent_post, paired = TRUE))
rep_comp_party1
rep_comp_party2 <- (t.test(df.comp.party2$represent_pre, df.comp.party2$represent_post, paired = TRUE))
rep_comp_party2
rep_comp_party3 <- (t.test(df.comp.party3$represent_pre, df.comp.party3$represent_post, paired = TRUE))
rep_comp_party3
rep_comp_party4 <- (t.test(df.comp.party4$represent_pre, df.comp.party4$represent_post, paired = TRUE))
rep_comp_party4
rep_comp_party5 <- (t.test(df.comp.party5$represent_pre, df.comp.party5$represent_post, paired = TRUE))
rep_comp_party5
rep_comp_party6 <- (t.test(df.comp.party6$represent_pre, df.comp.party6$represent_post, paired = TRUE))
rep_comp_party6
rep_comp_party7 <- (t.test(df.comp.party7$represent_pre, df.comp.party7$represent_post, paired = TRUE))
rep_comp_party7
rep_comp_party8 <- (t.test(df.comp.party8$represent_pre, df.comp.party8$represent_post, paired = TRUE))
rep_comp_party8


df.fail.party1 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "1") 
df.fail.party2 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "2") 
df.fail.party3 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "3") 
df.fail.party4 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "4") 
df.fail.party5 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "5") 
df.fail.party6 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "6") 
df.fail.party7 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "7") 
df.fail.party8 <- df1 %>%
  filter(df1$Condition == "Defection" & df1$party_pre == "8") 

rep_fail_party1 <- (t.test(df.fail.party1$represent_pre, df.fail.party1$represent_post, paired = TRUE))
rep_fail_party1
rep_fail_party2 <- (t.test(df.fail.party2$represent_pre, df.fail.party2$represent_post, paired = TRUE))
rep_fail_party2
rep_fail_party3 <- (t.test(df.fail.party3$represent_pre, df.fail.party3$represent_post, paired = TRUE))
rep_fail_party3
rep_fail_party4 <- (t.test(df.fail.party4$represent_pre, df.fail.party4$represent_post, paired = TRUE))
rep_fail_party4
rep_fail_party5 <- (t.test(df.fail.party5$represent_pre, df.fail.party5$represent_post, paired = TRUE))
rep_fail_party5
rep_fail_party6 <- (t.test(df.fail.party6$represent_pre, df.fail.party6$represent_post, paired = TRUE))
rep_fail_party6
rep_fail_party7 <- (t.test(df.fail.party7$represent_pre, df.fail.party7$represent_post, paired = TRUE))
rep_fail_party7
rep_fail_party8 <- (t.test(df.fail.party8$represent_pre, df.fail.party8$represent_post, paired = TRUE))
rep_fail_party8



df.failcomp.party1 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "1") 
df.failcomp.party2 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "2") 
df.failcomp.party3 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "3") 
df.failcomp.party4 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "4") 
df.failcomp.party5 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "5") 
df.failcomp.party6 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "6") 
df.failcomp.party7 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "7") 
df.failcomp.party8 <- df1 %>%
  filter(df1$Condition == "Defection + Compromise" & df1$party_pre == "8") 

rep_failcomp_party1 <- (t.test(df.failcomp.party1$represent_pre, df.failcomp.party1$represent_post, paired = TRUE))
rep_failcomp_party1
rep_failcomp_party2 <- (t.test(df.failcomp.party2$represent_pre, df.failcomp.party2$represent_post, paired = TRUE))
rep_failcomp_party2
rep_failcomp_party3 <- (t.test(df.failcomp.party3$represent_pre, df.failcomp.party3$represent_post, paired = TRUE))
rep_failcomp_party3
rep_failcomp_party4 <- (t.test(df.failcomp.party4$represent_pre, df.failcomp.party4$represent_post, paired = TRUE))
rep_failcomp_party4
rep_failcomp_party5 <- (t.test(df.failcomp.party5$represent_pre, df.failcomp.party5$represent_post, paired = TRUE))
rep_failcomp_party5
rep_failcomp_party6 <- (t.test(df.failcomp.party6$represent_pre, df.failcomp.party6$represent_post, paired = TRUE))
rep_failcomp_party6
rep_failcomp_party7 <- (t.test(df.failcomp.party7$represent_pre, df.failcomp.party7$represent_post, paired = TRUE))
rep_failcomp_party7
rep_failcomp_party8 <- (t.test(df.failcomp.party8$represent_pre, df.failcomp.party8$represent_post, paired = TRUE))
rep_failcomp_party8

rep_control_combined_results <- df.control %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(represent_pre, represent_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(rep_control_combined_results)

rep_comp_combined_results <- df.comp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(represent_pre, represent_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(rep_comp_combined_results)

rep_fail_combined_results <- df.fail %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(represent_pre, represent_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(rep_fail_combined_results)

rep_failcomp_combined_results <- df.failcomp %>%
  group_by(party_pre) %>%
  summarise(
    t_test = list(t.test(represent_pre, represent_post, paired = TRUE))
  ) %>%
  mutate(
    estimate = sapply(t_test, function(x) x$estimate),
    conf_low = sapply(t_test, function(x) x$conf.int[1]),
    conf_high = sapply(t_test, function(x) x$conf.int[2]),
    p_value = sapply(t_test, function(x) x$p.value)
  ) %>%
  select(party_pre, estimate, conf_low, conf_high, p_value)
print(rep_failcomp_combined_results)


#linear effects by party ##############################################################################################################################


#linear model
model3 <- lm(change_party ~ Condition*factor(party_pre),
             data = df1)
summary(model3)


#coefficient plot
l <- c("(Intercept)" = "Brothers of Italy",
       "ConditionCompromise" = "Brothers of Italy",
       "ConditionDefection" = "Brothers of Italy",
       "ConditionDefection + Compromise" = "Brothers of Italy",
       "party_pre8" = "+Europa",
       "party_pre7" = "Greens and Left Alliance",
       "party_pre6" = "Action - Italia Viva",
       "party_pre5" = "5 Star Movement",
       "party_pre4" = "Democratic Party - IDP",
       "party_pre3" = "Forza Italia",
       "party_pre2" = "Lega",
       "ConditionCompromise:party_pre8" = "+Europa",
       "ConditionCompromise:party_pre7" = "Greens and Left Alliance",
       "ConditionCompromise:party_pre6" = "Action - Italia Viva",
       "ConditionCompromise:party_pre5" = "5 Star Movement",
       "ConditionCompromise:party_pre4" = "Democratic Party - IDP",
       "ConditionCompromise:party_pre3" = "Forza Italia",
       "ConditionCompromise:party_pre2" = "Lega",
       "ConditionDefection:party_pre8" = "+Europa",
       "ConditionDefection:party_pre7" = "Greens and Left Alliance",
       "ConditionDefection:party_pre6" = "Action - Italia Viva",
       "ConditionDefection:party_pre5" = "5 Star Movement",
       "ConditionDefection:party_pre4" = "Democratic Party - IDP",
       "ConditionDefection:party_pre3" = "Forza Italia",
       "ConditionDefection:party_pre2" = "Lega",
       "ConditionDefection + Compromise:party_pre8" = "+Europa",
       "ConditionDefection + Compromise:party_pre7" = "Greens and Left Alliance",
       "ConditionDefection + Compromise:party_pre6" = "Action - Italia Viva",
       "ConditionDefection + Compromise:party_pre5" = "5 Star Movement",
       "ConditionDefection + Compromise:party_pre4" = "Democratic Party - IDP",
       "ConditionDefection + Compromise:party_pre3" = "Forza Italia",
       "ConditionDefection + Compromise:party_pre2" = "Lega")
df1$party_pre <- as.factor(df1$party_pre)

tidy_reg_conf_int3 <- df1 %>% 
  lm(change_party ~ Condition*party_pre,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int3

tidy_reg_conf_int3$condition <- ifelse(tidy_reg_conf_int3$term == "(Intercept)", 0,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise", 3,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre8", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre7", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre6", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre5", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre4", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre3", 0,
                                       ifelse(tidy_reg_conf_int3$term == "party_pre2", 0,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre8", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre7", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre6", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre5", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre4", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre3", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionCompromise:party_pre2", 1,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre8", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre7", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre6", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre5", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre4", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre3", 2,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection:party_pre2", 2, 
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre8", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre7", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre6", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre5", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre4", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre3", 3,
                                       ifelse(tidy_reg_conf_int3$term == "ConditionDefection + Compromise:party_pre2", 3, 
                                       5))))))))))))))))))))))))))))))))

tidy_reg_conf_int3_0 <- tidy_reg_conf_int3[tidy_reg_conf_int3$condition == 0,]
tidy_reg_conf_int3_1 <- tidy_reg_conf_int3[tidy_reg_conf_int3$condition == 1,]
tidy_reg_conf_int3_2 <- tidy_reg_conf_int3[tidy_reg_conf_int3$condition == 2,]
tidy_reg_conf_int3_3 <- tidy_reg_conf_int3[tidy_reg_conf_int3$condition == 3,]

plot3 <- tidy_reg_conf_int3 %>%
  ggplot(aes(x = estimate, 
             y = term,
             color = condition)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Coefficient plot with 95% CI's"
  ) +
  facet_grid(~condition) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3


plot3_0 <- tidy_reg_conf_int3_0 %>%
  ggplot(aes(x = estimate, 
             y = term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Control"
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3_0

plot3_1 <- tidy_reg_conf_int3_1 %>%
  ggplot(aes(x = estimate, 
             y = term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Compromise Condition",
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3_1

plot3_2 <- tidy_reg_conf_int3_2 %>%
  ggplot(aes(x = estimate, 
             y = term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Defection Condition",
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3_2

plot3_3 <- tidy_reg_conf_int3_3 %>%
  ggplot(aes(x = estimate, 
             y = term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Defection + Compromise Condition",
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3_3

grid.arrange(plot3_0, plot3_1,
             plot3_2, plot3_3, ncol=2)

#linear model with controls#####################################################
model4 <- lm(change_party ~ Condition*factor(party_pre) +
               age +
               gender +
               education +
               pol_interest,
             data = df1)
summary(model4)

#coefficient plot
tidy_reg_conf_int4 <- df1 %>% 
  lm(change_party ~ Condition*factor(party_pre) +
       age +
       gender +
       education +
       pol_interest,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int4


l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


plot4 <- tidy_reg_conf_int4 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term#,
                              #levels =rev(c("ConditionDefection + Compromise",
                              #              "ConditionDefection",
                              #              "ConditionCompromise",
                              #              "age",
                              #              "pol_interest",
                              #              "gender",
                              #              "education"))))
                              ))) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Coefficient plot with 95% CI's"
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot4

grid.arrange(plot3_0, plot3_1,
             plot3_2, plot3_3, ncol=2)

####################################################################################################
#between effects - support
df1$support_change <- ifelse(df1$support_post == df1$coalition_support_pr, 1, 0)

#linear model
model1.1 <- glm(support_change ~ Condition,
             data = df1,
             family =  binomial(link = "logit"))
summary(model1.1)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

tidy_reg_conf_int <- df1 %>% 
  glm(support_change ~ Condition,
     data = .,
     family =  binomial(link = "logit")) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int

plot1.1 <- tidy_reg_conf_int %>%
filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.1


df95_1.1 <- tidy(model1.1, conf.int = T,
               conf.level = 0.95)
df90_1.1 <- tidy(model1.1, conf.int = T,
               conf.level = 0.90)
coef_df_1.1 <- merge(df95_1.1, df90_1.1[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot1.1 <- coef_df_1.1 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.1

#linear model with controls
model2.1 <- glm(support_change ~ Condition +
                  rile_respondent_pre_1 +
                  rile_party_pre_1 +
                  Age +
                  Sex.num +
                  pol_interest +
                  Ethnicity.simplified.num +
                  Student.status.num +
                  Employment.status.num +
                  coalition_support_pr +
                  closeness_pre +
                  represent_pre,
             data = df1,
             family =  binomial(link = "logit"))
summary(model2.1)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  glm(support_change ~ Condition +
        rile_party_pre_1 +
       Age +
       Sex.num +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num,
     data = .,
     family =  binomial(link = "logit")) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_3 <- tidy(model2.1, conf.int = T,
               conf.level = 0.95)
df90_3 <- tidy(model2.1, conf.int = T,
               conf.level = 0.90)
coef_df_3 <- merge(df95_3, df90_3[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot2.1 <- coef_df_3 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2.1

grid.arrange(plot1.1, plot2.1, ncol=2,
             bottom = "Treatment Effect")


plot3.1  <- tidy_reg_conf_int2 %>%
filter(term != "(Intercept)") %>%
  filter(term != "rile_party_pre_1") %>%
  filter(term != "Age") %>%
  filter(term !="Sex.num") %>%
  filter(term != "Ethnicity.simplified.num") %>%
  filter(term != "Student.status.num") %>%
  filter(term!= "Employment.status.num") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot3.1

#############################################################################################################

#between effects - closeness
df1$closeness_change <- (df1$closeness_post- df1$closeness_pre)

#linear model
model1.2 <- lm(closeness_change ~ Condition,
                data = df1)
summary(model1.2)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

tidy_reg_conf_int <- df1 %>% 
  lm(closeness_change ~ Condition,
      data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int

plot1.2 <- tidy_reg_conf_int %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.2


df95_1.2 <- tidy(model1.2, conf.int = T,
                 conf.level = 0.95)
df90_1.2 <- tidy(model1.2, conf.int = T,
                 conf.level = 0.90)
coef_df_1.2 <- merge(df95_1.2, df90_1.2[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot1.2 <- coef_df_1.2 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.2

#linear model with controls
model2.2 <- lm(closeness_change ~ Condition +
                 rile_respondent_pre_1 +
                 rile_party_pre_1 +
                 Age +
                 Sex.num +
                 pol_interest +
                 Ethnicity.simplified.num +
                 Student.status.num +
                 Employment.status.num +
                 coalition_support_pr +
                 closeness_pre +
                 represent_pre,
                data = df1)
summary(model2.2)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  lm(closeness_change ~ Condition +
       rile_party_pre_1 +
        Age +
        Sex.num +
        Ethnicity.simplified.num +
        Student.status.num +
        Employment.status.num,
      data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_4 <- tidy(model2.2, conf.int = T,
               conf.level = 0.95)
df90_4 <- tidy(model2.2, conf.int = T,
               conf.level = 0.90)
coef_df_4 <- merge(df95_4, df90_4[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))



plot2.2 <- coef_df_4 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2.2

grid.arrange(plot1.2, plot2.2, ncol=2,
             bottom = "Treatment Effect")

plot2.3 <- tidy_reg_conf_int2 %>%
  filter(term != "(Intercept)") %>%
  filter(term != "rile_party_pre_1") %>%
  filter(term != "Age") %>%
  filter(term !="Sex.num") %>%
  filter(term != "Ethnicity.simplified.num") %>%
  filter(term != "Student.status.num") %>%
  filter(term!= "Employment.status.num") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2.3

##############################################################################################


#between effects - representativeness

df1$rep_change <- (df1$represent_pre - df1$represent_post)

#linear model
model1.3 <- lm(rep_change ~ Condition,
               data = df1)
summary(model1.3)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

tidy_reg_conf_int <- df1 %>% 
  lm(rep_change ~ Condition,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int

plot1.3 <- tidy_reg_conf_int %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.3


df95_1.3 <- tidy(model1.3, conf.int = T,
                 conf.level = 0.95)
df90_1.3 <- tidy(model1.3, conf.int = T,
                 conf.level = 0.90)
coef_df_1.3 <- merge(df95_1.3, df90_1.3[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot1.3 <- coef_df_1.3 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot1.3


#linear model with controls
model2.3 <- lm(rep_change ~ Condition +
                 rile_respondent_pre_1 +
                 rile_party_pre_1 +
                 Age +
                 Sex.num +
                 pol_interest +
                 Ethnicity.simplified.num +
                 Student.status.num +
                 Employment.status.num +
                 coalition_support_pr +
                 closeness_pre +
                 represent_pre,
               data = df1)
summary(model2.3)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  lm(rep_change ~ Condition +
       rile_party_pre_1 +
       Age +
       Sex.num +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2


l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_5 <- tidy(model2.3, conf.int = T,
               conf.level = 0.95)
df90_5 <- tidy(model2.3, conf.int = T,
               conf.level = 0.90)
coef_df_5 <- merge(df95_5, df90_5[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))



plot2.3 <- coef_df_5 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2.3

grid.arrange(plot1.3, plot2.3, ncol=2)


plot2.4 <- tidy_reg_conf_int2 %>%
  filter(term != "(Intercept)") %>%
  filter(term != "rile_party_pre_1") %>%
  filter(term != "Age") %>%
  filter(term !="Sex.num") %>%
  filter(term != "Ethnicity.simplified.num") %>%
  filter(term != "Student.status.num") %>%
  filter(term!= "Employment.status.num") %>%
  ggplot(aes(estimate, term)) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2.4

#################################################################################################################
#effects by far-right (binary)

df1$fr <- ifelse(df1$party_pre == 1 | df1$party_pre == 2, 
                 1, 0)

#linear model with controls#########################
fr.model2 <- lm(change_party ~ Condition*factor(fr) +
                  rile_respondent_pre_1 +
                  rile_party_pre_1 +
                  Age +
                  Sex.num +
                  pol_interest +
                  Ethnicity.simplified.num +
                  Student.status.num +
                  Employment.status.num +
                  coalition_support_pr +
                  closeness_pre +
                  represent_pre,
             data = df1)
summary(fr.model2)

#coefficient plot
tidy_reg_conf_int4 <- df1 %>% 
  lm(change_party ~ Condition*factor(fr) +
       rile_party_pre_1 +
       Age +
       Sex.num +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int4

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Pre",
       "age" = "Age",
       "pol_interest" = "Political Interest",
       "gender" = "Gender",
       "education" = "Education")

plot4 <- tidy_reg_conf_int4 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term#,
                              #levels =rev(c("ConditionDefection + Compromise",
                              #              "ConditionDefection",
                              #              "ConditionCompromise",
                              #              "age",
                              #              "pol_interest",
                              #              "gender",
                              #              "education"))))
  ))) +
  geom_point() +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "Treatment Effect",
    y = NULL,
    title = "Coefficient plot with 95% CI's"
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot4



##################################################################################################
#are coefficients statistically different?

library(car)
linearHypothesis(model2.3, c("ConditionDefection - ConditionDefection + Compromise = 0"))
linearHypothesis(model2.3, c("ConditionCompromise - ConditionDefection + Compromise = 0"))
linearHypothesis(model2.3, c("ConditionDefection - ConditionCompromise = 0"))






#############################################################################################
#differences between Increase and Decrease subconditions
df1$asylum <- ifelse(df1$asylum == 2, 1, 0)
df.decrease <- subset(df1, asylum == 1)
df.increase <- subset(df1, asylum == 0)
df.decrease %>%
  group_by(Condition) %>%
  summarize(count = n())

df.increase %>%
  group_by(Condition) %>%
  summarize(count = n())


#linear model with controls (decrease)
modeld1 <- lm(#change_party
              #closeness_change
              rep_change
              ~ Condition+
                rile_respondent_pre_1 +
                rile_party_pre_1 +
                Age +
                Sex.num +
                pol_interest +
                Ethnicity.simplified.num +
                Student.status.num +
                Employment.status.num +
                coalition_support_pr +
                closeness_pre +
                represent_pre,
              data = df.decrease)
summary(modeld1)


#linear model with controls (increase)
modeli1 <- lm(#change_party
              # closeness_change
               rep_change 
              ~ Condition+
                rile_respondent_pre_1 +
                rile_party_pre_1 +
                Age +
                Sex.num +
                pol_interest +
                Ethnicity.simplified.num +
                Student.status.num +
                Employment.status.num +
                coalition_support_pr +
                closeness_pre +
                represent_pre,
              data = df.increase)
summary(modeli1)




coef_dfi1 <- tidy(modeli1, conf.int = T,
                  conf.level = 0.95)
coef_dfi1$Subcondition <- "Increase"

coef_dfd1 <- tidy(modeld1, conf.int = T,
                  conf.level = 0.95)
coef_dfd1$Subcondition <- "Decrease"

coef_dfi1_90 <- tidy(modeli1, conf.int = T,
                     conf.level = 0.90)
coef_dfd1_90 <- tidy(modeld1, conf.int = T,
                     conf.level = 0.90)
coef_dfi1 <- merge(coef_dfi1, coef_dfi1_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
coef_dfd1 <- merge(coef_dfd1, coef_dfd1_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))

coef_dfid <- rbind(coef_dfi1, coef_dfd1)

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")



plotid1 <- coef_dfid %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                 levels =rev(c("ConditionDefection + Compromise",
                                                   "ConditionDefection",
                                                   "ConditionCompromise",
                                                   "rile_party_pre_1",
                                                   "rile_respondent_pre_1",
                                                   "pol_interest",
                                                   "Age",
                                                   "Sex.num",
                                                   "Ethnicity.simplified.num",
                                                   "Student.status.num",
                                                   "Employment.status.num",
                                                   "coalition_support_pr",
                                                   "closeness_pre",
                                                   "represent_pre"))))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.5)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.5)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  theme_minimal() +
  labs(title = "",
       y = "Covariates",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotid1


###########
#plot without controls
plotid2 <- coef_dfid %>%
  filter(term == "ConditionDefection + Compromise" | 
           term == "ConditionDefection" |
           term == "ConditionCompromise") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                                            levels =rev(c("ConditionDefection + Compromise",
                                                                          "ConditionDefection",
                                                                          "ConditionCompromise"
                                                                          ))))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.5)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.5)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  theme_minimal() +
  labs(title = "",
       y = "",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotid2





#########################
#linear model with controls  (interaction)
modela1 <- lm(change_party ~ Condition*factor(asylum) +
               rile_respondent_pre_1 +
               rile_party_pre_1 +
               Age +
               Sex.num +
               pol_interest +
               Ethnicity.simplified.num +
               Student.status.num +
               Employment.status.num +
               coalition_support_pr +
               closeness_pre +
               represent_pre,
               data = df1)
summary(modela1)


l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)",
       "factor(asylum)1" = "Decrease",
       "ConditionCompromise:factor(asylum)1" = "Compromise X Decrease",
       "ConditionDefection:factor(asylum)1" = "Defection X Decrease",
       "ConditionDefection + Compromise:factor(asylum)1" = "Defection + Compromise X Decrease")


df95_a1 <- tidy(modela1, conf.int = T,
               conf.level = 0.95)
df90_a1 <- tidy(modela1, conf.int = T,
               conf.level = 0.90)
coef_df_a1 <- merge(df95_a1, df90_a1[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plota1 <- coef_df_a1 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "factor(asylum)1",
                                            "ConditionCompromise:factor(asylum)1",
                                            "ConditionDefection:factor(asylum)1",
                                            "ConditionDefection + Compromise:factor(asylum)1",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plota1

grid.arrange(plot1, plot2, ncol=2,
             top = "Treatment Effects with 90% and 95% Confidence Intervals",
             bottom = "Treatment Effect") 





#########################################################
#difference between in government/opposition parties
df.govt <- subset(df1, party_pre == 1 | party_pre == 2 | party_pre == 3)
df.opposition <- subset(df1, party_pre != 1 & party_pre != 2 & party_pre != 3)

df.govt %>%
  group_by(Condition) %>%
  summarize(count = n())

df.opposition %>%
  group_by(Condition) %>%
  summarize(count = n())

#linear model with controls (in govt)
modelgovt <- lm(change_party
                #closeness_change
                 #rep_change  
                ~ Condition+
                rile_respondent_pre_1 +
                rile_party_pre_1 +
                Age +
                Sex.num +
                pol_interest +
                Ethnicity.simplified.num +
                Student.status.num +
                Employment.status.num +
                coalition_support_pr +
                closeness_pre +
                represent_pre,
              data = df.govt)
summary(modelgovt)


#linear model with controls (opposition)
modelopp <- lm(change_party
               # closeness_change
               #rep_change 
               ~ Condition+
                rile_respondent_pre_1 +
                rile_party_pre_1 +
                Age +
                Sex.num +
                pol_interest +
                Ethnicity.simplified.num +
                Student.status.num +
                Employment.status.num +
                coalition_support_pr +
                closeness_pre +
                represent_pre,
              data = df.opposition)
summary(modelopp)




coef_dfgovt <- tidy(modelgovt, conf.int = T,
                  conf.level = 0.95)
coef_dfgovt$Subcondition <- "In Government"

coef_dfopp <- tidy(modelopp, conf.int = T,
                  conf.level = 0.95)
coef_dfopp$Subcondition <- "Opposition"

coef_dfgovt_90 <- tidy(modelgovt, conf.int = T,
                     conf.level = 0.90)
coef_dfopp_90 <- tidy(modelopp, conf.int = T,
                     conf.level = 0.90)
coef_dfgovt <- merge(coef_dfgovt, coef_dfgovt_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
coef_dfopp <- merge(coef_dfopp, coef_dfopp_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))

coef_dfgovtopp <- rbind(coef_dfgovt, coef_dfopp)

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")



plotgovt1 <- coef_dfgovtopp %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                                            levels =rev(c("ConditionDefection + Compromise",
                                                                          "ConditionDefection",
                                                                          "ConditionCompromise",
                                                                          "rile_party_pre_1",
                                                                          "rile_respondent_pre_1",
                                                                          "pol_interest",
                                                                          "Age",
                                                                          "Sex.num",
                                                                          "Ethnicity.simplified.num",
                                                                          "Student.status.num",
                                                                          "Employment.status.num",
                                                                          "coalition_support_pr",
                                                                          "closeness_pre",
                                                                          "represent_pre"))))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.5)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.5)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  #scale_x_continuous(limits = c(-.5, .5)) + #use for rep_change score dv
  theme_minimal() +
  labs(title = "",
       y = "Covariates",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotgovt1


###########
#plot without controls
plotgovt2 <- coef_dfgovtopp %>%
  filter(term == "ConditionDefection + Compromise" | 
           term == "ConditionDefection" |
           term == "ConditionCompromise") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                                            levels =rev(c("ConditionDefection + Compromise",
                                                                          "ConditionDefection",
                                                                          "ConditionCompromise"
                                                            ))))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.5)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.5)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  theme_minimal() +
  labs(title = "",
       y = "",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotgovt2

##########################################
#coalition support change
#logit model with controls (in govt)
modelgovt <- glm(support_change 
  ~ Condition+
    rile_respondent_pre_1 +
    rile_party_pre_1 +
    Age +
    Sex.num +
    pol_interest +
    Ethnicity.simplified.num +
    Student.status.num +
    Employment.status.num +
    coalition_support_pr +
    closeness_pre +
    represent_pre,
  data = df.govt,
  family =  binomial(link = "logit"))
summary(modelgovt)


#linear model with controls (opposition)
modelopp <- glm(support_change 
  ~ Condition+
    rile_respondent_pre_1 +
    rile_party_pre_1 +
    Age +
    Sex.num +
    pol_interest +
    Ethnicity.simplified.num +
    Student.status.num +
    Employment.status.num +
    coalition_support_pr +
    closeness_pre +
    represent_pre,
  data = df.opposition,
  family =  binomial(link = "logit"))
summary(modelopp)




coef_dfgovt <- tidy(modelgovt, conf.int = T,
                    conf.level = 0.95)
coef_dfgovt$Subcondition <- "In Government"

coef_dfopp <- tidy(modelopp, conf.int = T,
                   conf.level = 0.95)
coef_dfopp$Subcondition <- "Opposition"

coef_dfgovt_90 <- tidy(modelgovt, conf.int = T,
                       conf.level = 0.90)
coef_dfopp_90 <- tidy(modelopp, conf.int = T,
                      conf.level = 0.90)
coef_dfgovt <- merge(coef_dfgovt, coef_dfgovt_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
coef_dfopp <- merge(coef_dfopp, coef_dfopp_90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))

coef_dfgovtopp <- rbind(coef_dfgovt, coef_dfopp)

estimates <- coef_dfgovt %>%
  mutate(odds_ratio = exp(estimate),
         conf.low_95 = exp(conf.low_95),
         conf.high_95 = exp(conf.high_95),
         conf.low_90 = exp(conf.low_90),
         conf.high_90 = exp(conf.high_90))

estimates2 <- coef_dfopp %>%
  mutate(odds_ratio = exp(estimate),
         conf.low_95 = exp(conf.low_95),
         conf.high_95 = exp(conf.high_95),
         conf.low_90 = exp(conf.low_90),
         conf.high_90 = exp(conf.high_90))

coef_dfgovtopp <- rbind(estimates, estimates2)

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")



plotgovt1 <- coef_dfgovtopp %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                                            levels =rev(c("ConditionDefection + Compromise",
                                                                          "ConditionDefection",
                                                                          "ConditionCompromise",
                                                                          "rile_party_pre_1",
                                                                          "rile_respondent_pre_1",
                                                                          "pol_interest",
                                                                          "Age",
                                                                          "Sex.num",
                                                                          "Ethnicity.simplified.num",
                                                                          "Student.status.num",
                                                                          "Employment.status.num",
                                                                          "coalition_support_pr",
                                                                          "closeness_pre",
                                                                          "represent_pre"))))) +
  geom_point(position = position_dodge(width = 0.1), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.1)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.1)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  scale_x_continuous(limits = c(-.8, .8)) + #use for rep_change score dv
  theme_minimal() +
  labs(title = "",
       y = "Covariates",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotgovt1


###########
#plot without controls
plotgovt2 <- coef_dfgovtopp %>%
  filter(term == "ConditionDefection + Compromise" | 
           term == "ConditionDefection" |
           term == "ConditionCompromise") %>%
  ggplot(aes(color = Subcondition, x = estimate, y = factor(term,
                                                            levels =rev(c("ConditionDefection + Compromise",
                                                                          "ConditionDefection",
                                                                          "ConditionCompromise"
                                                            ))))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), 
                width = 0.2, linetype = "dashed", 
                position = position_dodge(width = 0.5)) +  # 95% CI solid line
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), 
                width = 0.2, linetype = "solid", 
                position = position_dodge(width = 0.5)) +  # 90% CI dashed line
  geom_vline(xintercept = 0, lty = 2) +
  theme_minimal() +
  labs(title = "",
       y = "",
       x = "Treatment Effect") +
  scale_y_discrete(labels = l) +
  theme(legend.position = "bottom")
plotgovt2








#######################################################################
#manipulation check, treatment manipulation
df1$manipulation_treat[df1$manipulation_treat == ""] <- 0
df1$manipulation_treat <- ifelse(df1$manipulation_treat == 0, 0,
                          ifelse(df1$manipulation_treat == 1, 1, 
                          ifelse(df1$manipulation_treat == 2, 1,
                                        NA)))
df.manip <- df1
df.manip <- df.manip[!is.na(df.manip$manipulation_treat),]

#linear model
model1 <- lm(change_party ~ Condition,
             data = df.manip)
summary(model1)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

#plot with 90% and 95% CI's

df95 <- tidy(model1, conf.int = T,
             conf.level = 0.95)
df90 <- tidy(model1, conf.int = T,
             conf.level = 0.90)
coef_df <- merge(df95, df90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
plot1<- coef_df %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(x = estimate, y = term)) +
  geom_point(size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +# 90% CI  # Flip for horizontal plot
  labs(title = "",
       x = "",
       y = "") +
  scale_y_discrete(labels = l) +
  theme_classic() 
plot1



#linear model with controls
model2 <- lm(change_party ~ Condition +
               rile_respondent_pre_1 +
               rile_party_pre_1 +
               Age +
               Sex.num +
               pol_interest +
               Ethnicity.simplified.num +
               Student.status.num +
               Employment.status.num +
               coalition_support_pr +
               closeness_pre +
               represent_pre,
             data = df.manip)
summary(model2)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  lm(change_party ~ Condition +
       rile_respondent_pre_1 +
       rile_party_pre_1 +
       Age +
       Sex.num +
       pol_interest +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num +
       coalition_support_pr +
       closeness_pre +
       represent_pre,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_2 <- tidy(model2, conf.int = T,
               conf.level = 0.95)
df90_2 <- tidy(model2, conf.int = T,
               conf.level = 0.90)
coef_df_2 <- merge(df95_2, df90_2[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot2 <- coef_df_2 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2

grid.arrange(plot1, plot2, ncol=2,
             top = "",
             bottom = "Treatment Effect") 


#############################
#manipulation check, attention check
df1$manipulation_color <- ifelse(df1$manipulation_color == 3, 1, 0)
df.manip <- df1 %>%
  filter(manipulation_color == 1)

#linear model
model1 <- lm(change_party ~ Condition,
             data = df.manip)
summary(model1)


#coefficient plot
l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise")

#plot with 90% and 95% CI's

df95 <- tidy(model1, conf.int = T,
             conf.level = 0.95)
df90 <- tidy(model1, conf.int = T,
             conf.level = 0.90)
coef_df <- merge(df95, df90[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))
plot1<- coef_df %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(x = estimate, y = term)) +
  geom_point(size = 2) +  # Plot the coefficient estimates
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +# 90% CI  # Flip for horizontal plot
  labs(title = "",
       x = "",
       y = "") +
  scale_y_discrete(labels = l) +
  theme_classic() 
plot1



#linear model with controls
model2 <- lm(change_party ~ Condition +
               rile_respondent_pre_1 +
               rile_party_pre_1 +
               Age +
               Sex.num +
               pol_interest +
               Ethnicity.simplified.num +
               Student.status.num +
               Employment.status.num +
               coalition_support_pr +
               closeness_pre +
               represent_pre,
             data = df.manip)
summary(model2)

#coefficient plot
tidy_reg_conf_int2 <- df1 %>% 
  lm(change_party ~ Condition +
       rile_respondent_pre_1 +
       rile_party_pre_1 +
       Age +
       Sex.num +
       pol_interest +
       Ethnicity.simplified.num +
       Student.status.num +
       Employment.status.num +
       coalition_support_pr +
       closeness_pre +
       represent_pre,
     data = .) %>% 
  tidy(conf.int = TRUE)
tidy_reg_conf_int2

l <- c("ConditionCompromise" = "Compromise",
       "ConditionDefection" = "Defection",
       "ConditionDefection + Compromise" = "Defection + Compromise",
       "rile_party_pre_1" = "RILE Party (pre)",
       "rile_respondent_pre_1" = "RILE Respondent (pre)",
       "Age" = "Age",
       "pol_interest" = "Political Interest",
       "Sex.num" = "Sex",
       "Ethnicity.simplified.num" = "Ethnicity",
       "Student.status.num" = "Student",
       "Employment.status.num" = "Employment",
       "coalition_support_pr" = "Coalition Support (pre)",
       "closeness_pre" = "Party Closeness (pre)",
       "represent_pre" = "Party Representativeness (pre)")


df95_2 <- tidy(model2, conf.int = T,
               conf.level = 0.95)
df90_2 <- tidy(model2, conf.int = T,
               conf.level = 0.90)
coef_df_2 <- merge(df95_2, df90_2[, c("term", "conf.low", "conf.high")], by = "term", suffixes = c("_95", "_90"))


plot2 <- coef_df_2 %>%
  filter(term != "(Intercept)") %>%
  ggplot(aes(estimate, factor(term,
                              levels =rev(c("ConditionDefection + Compromise",
                                            "ConditionDefection",
                                            "ConditionCompromise",
                                            "rile_party_pre_1",
                                            "rile_respondent_pre_1",
                                            "pol_interest",
                                            "Age",
                                            "Sex.num",
                                            "Ethnicity.simplified.num",
                                            "Student.status.num",
                                            "Employment.status.num",
                                            "coalition_support_pr",
                                            "closeness_pre",
                                            "represent_pre"))))) +
  geom_point() +
  geom_errorbar(aes(xmin = conf.low_95, xmax = conf.high_95), width = 0.2, linetype = "dashed") +  # 95% CI
  geom_errorbar(aes(xmin = conf.low_90, xmax = conf.high_90), width = 0.2, linetype = "solid") +
  # add in a dotted line at zero
  geom_vline(xintercept = 0, lty = 2) +
  labs(
    x = "",
    y = NULL,
    title = ""
  ) +
  scale_y_discrete(labels = l) +
  theme_classic()
plot2

grid.arrange(plot1, plot2, ncol=2,
             top = "",
             bottom = "Treatment Effect") 
